#!/usr/bin/python
# -*- coding: UTF-8 -*-

"""
@author: jiu
@contact: 643562684@qq.com
@time: 2022/3/10 14:11
"""

from xlrd import open_workbook
from xlutils.copy import copy
from common.base_read_file import BASE_FILE


class Read_XLSX(BASE_FILE):
    def __init__(self, exls_path: str, sheet: [str, int] = 0, title_exists: bool = False):
        super(Read_XLSX, self).__init__(exls_path)
        self._title_exists = title_exists

        if not isinstance(sheet, (str, int)):
            raise TypeError("type  error {}".format(sheet))

        self.__sheet = sheet
        self._data = []
        self._sheet = self._workbook(self._file, self.__sheet)

    # @property
    # def sheet(self):
    #     return self.__sheet
    #
    # @sheet.setter
    # def sheet(self, val):
    #     if not isinstance(val, (str, int)):
    #         raise TypeError("传递的类型的无法识别{}".format(val))
    #
    #     self.__sheet = val

    def _workbook(self, file_path: str, sheet: [int, str]):
        """
            sheet Page operation object single page sheet
        :param file_path:
        :param sheet:
        :return:
        """
        work_book = open_workbook(file_path, sheet)
        _sheet = None
        if isinstance(sheet, int):
            _sheet = work_book.sheet_by_index(sheet)
        if isinstance(sheet, str):
            _sheet = work_book.sheet_by_name(sheet)

        return _sheet

    @property
    def data(self):
        """
           read xlsx all information
        :return:
        """
        if not self._data:
            if not self._title_exists:
                for col in range(0, self._sheet.nrows):
                    self._data.append(self._sheet.row_values(col))
            else:
                title = self._sheet.row_values(0)
                for col in range(1, self._sheet.nrows):
                    self._data.append(
                        dict(zip(title, self._sheet.row_values(col)))
                    )

        return self._data

    def get_sheet_cell(self, row, col):
        """
           Gets the specified row and column
        :param row:
        :param col:
        :return:
        """
        cell_data = self._sheet.cell_value(row, col)

        return cell_data

    def get_rows(self):
        """
            Number of rows in xlsx table
        :return:
        """
        return self._sheet.nrows

    def get_cols(self):
        """
           Number of column in xlsx table
        :return:
        """
        return self._sheet.ncols

    def get_write_data(self, row, col, val):
        """
            To write xlsx, you need to copy the file first. When executing,
            you need to ensure that the target xlsx is closed
        :param row:
        :param col:
        :param val:
        :return:
        """
        _data = open_workbook(self._file)
        copy_data = copy(_data)
        # 选择写入的 sheet 页
        copy_data_sheet = copy_data.get_sheet(self.__sheet)
        # 写入数据
        copy_data_sheet.write(row, col, val)
        # 保存数据
        copy_data.save(self._file)

